﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using ESRI.ArcGIS.ArcCatalogUI;
using ESRI.ArcGIS.ArcMap;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.CartoUI;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Desktop;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.GeoDatabaseUI;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.LocationUI;
using ESRI.ArcGIS.DataSourcesOleDB;
using System.IO;
using ConnectWithR.precipitation;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.AnalysisTools;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesGDB;
using System.Threading;
using ESRI.ArcGIS.SpatialAnalyst;
using ESRI.ArcGIS.GeoAnalyst;
using ESRI.ArcGIS.DataSourcesRaster;



namespace ConnectWithR
{
    ///<summary>
    ///Methods to select data sites, spatial analysis, table creation.
    ///</summary>
    class DataUtility
    {
        private static int stationNumber = 0; 
        private static int seriesID = 0;

        /// <summary>
        /// Get the feature within which the stations are
        /// </summary>
        /// <param name="focusedLayer">layer from which the feature is</param>
        /// <returns>feature that used to select stations</returns>
        public static IFeature selectFeatureForSelection(ILayer focusedLayer)
        {
            if (focusedLayer == null)
            {
                MessageBox.Show("please choose a layer where you would like to find a station");
                return null;
            }
            IEnumIDs selectedIDs = null;
            IFeature iSelectedFeature = null;
            IFeatureLayer iMyLayer = (IFeatureLayer)focusedLayer;
            IFeatureSelection pFeaSelection = (IFeatureSelection)iMyLayer;
            if (pFeaSelection.SelectionSet.Count == 0)
            {
                MessageBox.Show("please select a feature for searching");
                return null;
            }

            if (pFeaSelection.SelectionSet.Count > 1)
            {
                MessageBox.Show("only one feature of your selections will be used");
                return null;
            }
            IFeatureClass iFeaClassEntire = iMyLayer.FeatureClass;
            selectedIDs = pFeaSelection.SelectionSet.IDs;
            int oid = selectedIDs.Next();
            iSelectedFeature = iFeaClassEntire.GetFeature(oid);
            return iSelectedFeature;
        }

        /// <summary>
        /// Create table of all stations
        /// </summary>
        /// <param name="tableName">the table name which determined by the data type</param>
        /// <returns>station table</returns>
        public static ITable createStationTable(String tableName)
        {
            ITable pTable = null;
            IWorkspaceFactory pFact = new ShapefileWorkspaceFactory();
            IWorkspace pWorkspace = pFact.OpenFromFile("", 0);
            IFeatureWorkspace pFeatws = (IFeatureWorkspace)pWorkspace;
            tableName = tableName + "SiteInfo.dbf";

            if (File.Exists(tableName))
            {
                pTable = pFeatws.OpenTable(tableName);
                return pTable;
            }

            UID uid = new UIDClass();
            uid.Value = "esriGeoDatabase.Object";

            IFields fields = new Fields();
            ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

            IField field1 = new Field();
            IFieldEdit fieldEdit1 = (IFieldEdit)field1;

            IField field2 = new Field();
            IFieldEdit fieldEdit2 = (IFieldEdit)field2;

            IField field3 = new Field();
            IFieldEdit fieldEdit3 = (IFieldEdit)field3;

            IField field4 = new Field();
            IFieldEdit fieldEdit4 = (IFieldEdit)field4;

            IField field5 = new Field();
            IFieldEdit fieldEdit5 = (IFieldEdit)field5;

            fieldEdit1.Name_2 = "Object_id";
            fieldEdit1.Type_2 = esriFieldType.esriFieldTypeOID;
            fieldEdit1.IsNullable_2 = false;
            fieldEdit1.DefaultValue_2 = "0";
            fieldEdit1.Editable_2 = true;


            fieldEdit2.Name_2 = "FID";
            fieldEdit2.Type_2 = esriFieldType.esriFieldTypeInteger;
            fieldEdit2.IsNullable_2 = false;
            fieldEdit2.DefaultValue_2 = "0";
            fieldEdit2.Editable_2 = true;
            fieldEdit2.Length_2 = 100;

            fieldEdit3.Name_2 = "X";
            fieldEdit3.Type_2 = esriFieldType.esriFieldTypeDouble;
            fieldEdit3.IsNullable_2 = true;
            fieldEdit3.DefaultValue_2 = "0";
            fieldEdit3.Editable_2 = true;
            fieldEdit3.Length_2 = 20;

            fieldEdit4.Name_2 = "Y";
            fieldEdit4.Type_2 = esriFieldType.esriFieldTypeDouble;
            fieldEdit4.IsNullable_2 = true;
            fieldEdit4.DefaultValue_2 = "0";
            fieldEdit4.Editable_2 = true;
            fieldEdit4.Length_2 = 20;

            fieldEdit5.Name_2 = "siteCode";
            fieldEdit5.Type_2 = esriFieldType.esriFieldTypeString;
            fieldEdit5.IsNullable_2 = true;
            fieldEdit5.DefaultValue_2 = "0";
            fieldEdit5.Editable_2 = true;
            fieldEdit5.Length_2 = 40;

            fieldsEdit.FieldCount_2 = 5;

            fieldsEdit.set_Field(0, fieldEdit1);
            fieldsEdit.set_Field(1, fieldEdit2);
            fieldsEdit.set_Field(2, fieldEdit3);
            fieldsEdit.set_Field(3, fieldEdit4);
            fieldsEdit.set_Field(4, fieldEdit5);

            fields = (IFields)fieldsEdit;

            IFieldChecker fieldChecker = new FieldChecker();
            IEnumFieldError enumFieldError = null;
            IFields validateFields = null;
            fieldChecker.ValidateWorkspace = pWorkspace;
            fieldChecker.Validate(fields, out enumFieldError, out validateFields);

            pTable = pFeatws.CreateTable(tableName, validateFields, uid, null, "");

            MessageBox.Show("table created");

            return pTable;
        }

        /// <summary>
        /// Create layer of all staions from which the desired staions are selected
        /// </summary>
        /// <param name="pTable">table of all stations</param>
        /// <param name="dataType">data type</param>
        /// <returns>station layer</returns>
        public static IFeatureLayer createStationsLayer(ITable pTable, String dataType)
        {
            IDataset pDataSet;
            IName pTableName;
            pDataSet = (IDataset)pTable;
            pTableName = pDataSet.FullName;
            //label1.Text = pTableName.NameString;

            IXYEvent2FieldsProperties pXYEvent2FieldProperies;
            pXYEvent2FieldProperies = (IXYEvent2FieldsProperties)new XYEvent2FieldsProperties();
            pXYEvent2FieldProperies.YFieldName = "x";
            pXYEvent2FieldProperies.XFieldName = "y";
            pXYEvent2FieldProperies.ZFieldName = "";

            ISpatialReferenceFactory pSpatialReferenceFactory;
            pSpatialReferenceFactory = new SpatialReferenceEnvironment();
            IGeographicCoordinateSystem pGeographicCoordinateSystem;
            //pProjectedCoordinateSystem = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(26911);
            pGeographicCoordinateSystem = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(4269);
            //esriSRGeoCS_NAD1983	4269	North American Datum 1983.

            IXYEventSourceName pXYEventSourceName = (IXYEventSourceName)new XYEventSourceName();
            IName pXYName;
            IXYEventSource pXYEventSource;
            pXYEventSourceName.EventProperties = pXYEvent2FieldProperies;
            pXYEventSourceName.SpatialReference = pGeographicCoordinateSystem;
            pXYEventSourceName.EventTableName = pTableName;
            pXYName = (IName)pXYEventSourceName;
            pXYEventSource = (IXYEventSource)pXYName.Open();

            IFeatureLayer stationsLayer = new FeatureLayer();
            stationsLayer.FeatureClass = (IFeatureClass)pXYEventSource;
            stationsLayer.Name = dataType + " stations";

            return stationsLayer;
        }

        /// <summary>
        /// Export station layer to shapfile.
        /// </summary>
        /// <param name="pFeaLayer">station layer</param>
        public static void exportLayer(IFeatureLayer pFeaLayer)
        {
            if (File.Exists(pFeaLayer.Name + ".shp"))
            {
                return;
            }
            ISpatialReferenceFactory pSpatialReferenceFactory;
            pSpatialReferenceFactory = new SpatialReferenceEnvironment();
            IGeographicCoordinateSystem pGeographicCoordinateSystem;
            pGeographicCoordinateSystem = pSpatialReferenceFactory.CreateGeographicCoordinateSystem(4269);
            SpatialReferenceEnvironment pSpatialReferenceEnv = new SpatialReferenceEnvironment();
            ISpatialReference2 pSpatialReference = (ISpatialReference2)pGeographicCoordinateSystem;
            

            //output setting
            IWorkspaceFactory targetWorkSpaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace targetWorkSpace = targetWorkSpaceFactory.OpenFromFile("", 0);
            IDataset DatasetTarget = (IDataset)targetWorkSpace;
            IName NameTarget = DatasetTarget.FullName;
            IWorkspaceName workspaceName = (IWorkspaceName)NameTarget;

            //input setting
            IFeatureClass pFeatureClass = pFeaLayer.FeatureClass;
            IGeometryDef pGeometryDef = new GeometryDef();
            IGeometryDefEdit pGeometryDefEdit = (IGeometryDefEdit)pGeometryDef;
            pGeometryDefEdit.GeometryType_2 = pFeatureClass.ShapeType;
            pGeometryDefEdit.SpatialReference_2 = pSpatialReference;
            IDataset pInputDataSet = (IDataset)pFeatureClass;
            IFeatureClassName pFeaClassName = (IFeatureClassName)pInputDataSet.FullName;
            //IWorkspace pInWorkspace = pInputDataSet.Workspace;

            IFeatureClassName pOutFeatureClassName = (IFeatureClassName)new FeatureClassName();
            IDatasetName pDatasetName = (IDatasetName)pOutFeatureClassName;
            pDatasetName.Name = pFeaLayer.Name;
            pDatasetName.WorkspaceName = workspaceName;

            IExportOperation pExportOperation = new ExportOperation();

            pExportOperation.ExportFeatureClass((IDatasetName)pFeaClassName, null, null, pGeometryDef, pOutFeatureClassName, 0);
        }

        /// <summary>
        /// Add staion layer from disc
        /// </summary>
        /// <param name="dataType">data type</param>
        /// <returns></returns>
        public static IFeatureLayer addShapeFile(String dataType)
        {
            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile("", 0);
            IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(dataType);
            IFeatureLayer featureLayer = new FeatureLayer();
            featureLayer.FeatureClass = featureClass;
            featureLayer.Name = dataType + "Stations";
            featureLayer.Visible = true;
            return featureLayer;
        }

        /// <summary>
        /// select desired staions
        /// </summary>
        /// <param name="stationsLayer">layer of all stations</param>
        /// <param name="sourceFeature">feature within which the desired staions are located</param>
        /// <param name="dataType">data type</param>
        /// <param name="expandIndex1">index to expand the extent to search precipitation station</param>
        /// <param name="expandIndex2">index to expand the extent to search precipitation station</param>
        /// <param name="isPrecipitation">check if it is precipitation data</param>
        /// <param name="area">the area of selected feature</param>
        /// <returns>selected stations layer</returns>
        public static IFeatureLayer selectStations(IFeatureLayer stationsLayer, IFeature sourceFeature, String dataType, double expandIndex1, double expandIndex2, Boolean isPrecipitation, out Double area)
        {
            stationNumber++;
            IFeatureClass iFeaClassXY = stationsLayer.FeatureClass;
            IFeatureSelection iFeaSelectXY = (IFeatureSelection)stationsLayer;
            ISpatialFilter iSpaFilterXY = new SpatialFilter();
            IPolygon pPolygon = null;
            area = 0.0;
            if (!(sourceFeature.Shape is IPolygon))
            {
                MessageBox.Show("Please select the layer which contains the selected feature!");
                return null;
            }
            pPolygon = (IPolygon)sourceFeature.Shape;
            IEnvelope pEnvelope = pPolygon.Envelope;
            ISpatialFilter pSpatialFilter = new SpatialFilter();

            //Get the drainage area
            IArea pArea = sourceFeature.Shape as IArea;
            area = pArea.Area;

            pEnvelope.Expand(expandIndex1, expandIndex2, true);

            //Do exact search if ET0 is selected. Otherwise, use envolpe to search! 
            if (isPrecipitation)
            {
                pSpatialFilter.Geometry = pEnvelope;
            }
            else
            {
                pSpatialFilter.Geometry = pPolygon;
            }
            pSpatialFilter.GeometryField = iFeaClassXY.ShapeFieldName;
            pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

            iFeaSelectXY.SelectFeatures(pSpatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);

            int count = iFeaSelectXY.SelectionSet.Count;
            MessageBox.Show(Convert.ToString(count) + " site(s) have been found");

            if (count == 0)
            {
                return null;
            }

            IFeatureLayerDefinition iDefinitionXYFeature = (IFeatureLayerDefinition)stationsLayer;

            IFeatureLayer iSelectedXYLayer = iDefinitionXYFeature.CreateSelectionLayer(dataType + " Site" + Convert.ToString(stationNumber), true, "", "");

            return iSelectedXYLayer;
        }

        /// <summary>
        /// create time series data table
        /// </summary>
        /// <param name="tableName">table name</param>
        /// <param name="tableLayerName">table name in the TOC</param>
        /// <returns>time series data table</returns>
        public static ITable createTimeSeriesTable(String tableName, out String tableLayerName)
        {
            seriesID++;
            ITable pTable = null;
            IWorkspaceFactory pFact = new ShapefileWorkspaceFactory();
            IWorkspace pWorkspace = pFact.OpenFromFile("", 0);
            IFeatureWorkspace pFeatws = (IFeatureWorkspace)pWorkspace;
            tableLayerName = tableName + "TimeSeries" + Convert.ToString(seriesID);
            tableName = tableName + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".dbf";
            MessageBox.Show(tableName);
            

            if (File.Exists(tableName))
            {
                pTable = pFeatws.OpenTable(tableName);
                return pTable;
            }

            UID uid = new UIDClass();
            uid.Value = "esriGeoDatabase.Object";

            IFields fields = new Fields();
            ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

            IField field1 = new Field();
            IFieldEdit fieldEdit1 = (IFieldEdit)field1;

            IField field2 = new Field();
            IFieldEdit fieldEdit2 = (IFieldEdit)field2;

            IField field3 = new Field();
            IFieldEdit fieldEdit3 = (IFieldEdit)field3;

            IField field4 = new Field();
            IFieldEdit fieldEdit4 = (IFieldEdit)field4;

            IField field5 = new Field();
            IFieldEdit fieldEdit5 = (IFieldEdit)field5;

            fieldEdit1.Name_2 = "SiteCode";
            fieldEdit1.Type_2 = esriFieldType.esriFieldTypeString;
            fieldEdit1.IsNullable_2 = false;
            fieldEdit1.DefaultValue_2 = "0";
            fieldEdit1.Editable_2 = true;


            fieldEdit2.Name_2 = "TSTypeID";
            fieldEdit2.Type_2 = esriFieldType.esriFieldTypeString;
            fieldEdit2.IsNullable_2 = false;
            fieldEdit2.DefaultValue_2 = "0";
            fieldEdit2.Editable_2 = true;
            fieldEdit2.Length_2 = 100;

            fieldEdit3.Name_2 = "TSDateTime";
            fieldEdit3.Type_2 = esriFieldType.esriFieldTypeString;
            fieldEdit3.IsNullable_2 = true;
            fieldEdit3.DefaultValue_2 = "0";
            fieldEdit3.Editable_2 = true;
            fieldEdit3.Length_2 = 20;

            fieldEdit4.Name_2 = "TSUnit";
            fieldEdit4.Type_2 = esriFieldType.esriFieldTypeString;
            fieldEdit4.IsNullable_2 = true;
            fieldEdit4.DefaultValue_2 = "0";
            fieldEdit4.Editable_2 = true;
            fieldEdit4.Length_2 = 20;

            fieldEdit5.Name_2 = "TSValue";
            fieldEdit5.Type_2 = esriFieldType.esriFieldTypeDouble;
            fieldEdit5.IsNullable_2 = true;
            fieldEdit5.DefaultValue_2 = "0";
            fieldEdit5.Editable_2 = true;
            fieldEdit5.Length_2 = 40;

            fieldsEdit.FieldCount_2 = 5;

            fieldsEdit.set_Field(0, fieldEdit1);
            fieldsEdit.set_Field(1, fieldEdit2);
            fieldsEdit.set_Field(2, fieldEdit3);
            fieldsEdit.set_Field(3, fieldEdit4);
            fieldsEdit.set_Field(4, fieldEdit5);

            fields = (IFields)fieldsEdit;

            IFieldChecker fieldChecker = new FieldChecker();
            IEnumFieldError enumFieldError = null;
            IFields validateFields = null;
            fieldChecker.ValidateWorkspace = pWorkspace;
            fieldChecker.Validate(fields, out enumFieldError, out validateFields);

            pTable = pFeatws.CreateTable(tableName, validateFields, uid, null, "");
            return pTable;
        }

        /// <summary>
        /// add table to TOC
        /// </summary>
        /// <param name="pMap">current map</param>
        /// <param name="pTable">table to be added</param>
        /// <param name="tableLayerName">table name appearing in the TOC</param>
        public static void addTable(IMap pMap, ITable pTable, String tableLayerName)
        {
            if (pTable.RowCount(null) == 0)
            {
                return;
            }
            IStandaloneTableCollection pStTabCol;
            IStandaloneTable pStandaloneTable = new StandaloneTable();
            pStTabCol = (IStandaloneTableCollection)pMap;
            pStandaloneTable.Table = pTable;
            pStandaloneTable.Name = tableLayerName;
            pStTabCol.AddStandaloneTable(pStandaloneTable);
            MessageBox.Show("Time Series Table: " + tableLayerName + " has been added to ArcMap!");
        }

        /// <summary>
        /// clip DEM base on selected feature
        /// </summary>
        /// <param name="polygonLayer">selected feature based on which the DEM is clipped</param>
        /// <param name="DEMlayer">DEM raster layer</param>
        /// <param name="area">acreage of the selected feature</param>
        /// <returns>clipped DEM</returns>
        public static IRasterLayer clipDEM(IFeatureLayer polygonLayer, IRasterLayer DEMlayer, out Double area)
        {
            area = 0.0;
            if (polygonLayer == null)
            {
                MessageBox.Show("please select a layer that contains the polygon you want to use");
                return null;
            }
            if (DEMlayer == null)
            {
                MessageBox.Show("please select the input DEM layer");
                return null;
            }
            IFeatureSelection pFeaSelection = (IFeatureSelection)polygonLayer;
            IFeatureClass iFeaClassEntire = polygonLayer.FeatureClass;
            if (pFeaSelection.SelectionSet.Count == 0 || pFeaSelection.SelectionSet.Count > 1)
            {
                MessageBox.Show("please select one polygon");
                return null;
            }
            IEnumIDs selectedIDs = pFeaSelection.SelectionSet.IDs;
            
            int oid = selectedIDs.Next();
            IFeature iSelectedFeature = iFeaClassEntire.GetFeature(oid);
            IPolygon pPolygon = (IPolygon)iSelectedFeature.Shape;

            //Get the drainage area
            IArea pArea = iSelectedFeature.Shape as IArea;
            area = pArea.Area;

            IExtractionOp2 peop = (IExtractionOp2)new RasterExtractionOp();
            IRasterAnalysisEnvironment praenv = (IRasterAnalysisEnvironment)peop;
            IRasterProps irp = (IRasterProps)DEMlayer.Raster;
            IPnt csize = irp.MeanCellSize();
            praenv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, csize.X);
            praenv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, pPolygon.Envelope);
            //IGeoDataset pgds = peop.Rectangle((IGeoDataset)DEMlayer.Raster, iSelectedFeature.Shape.Envelope, true);
            IGeoDataset pgds = null;
            try
            {
                pgds = peop.Polygon((IGeoDataset)DEMlayer.Raster, pPolygon, true);
            }
            catch(System.Runtime.InteropServices.COMException e)
            {
                MessageBox.Show("Please select only one polygon");
                return null;
            }
            
            IRaster pExtractedRaster = (IRaster)pgds;
            IRasterLayer pRasterLayer = new RasterLayer();
            pRasterLayer.CreateFromRaster(pExtractedRaster);
            return pRasterLayer;

        }

        /// <summary>
        /// get the layer that selected by the combo
        /// </summary>
        /// <param name="pMap">current map</param>
        /// <param name="layerName">text of selected combo item</param>
        /// <returns>layer whose name equals the text</returns>
        public static ILayer getCBOLayer(IMap pMap, String layerName)
        {
            ILayer layer = null;
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                if (layerName == pMap.get_Layer(i).Name)
                {
                    layer = pMap.get_Layer(i);
                }
            }
            return layer;
        }

        /// <summary>
        /// get the table that selected by the combo
        /// </summary>
        /// <param name="pMap">current map</param>
        /// <param name="layerName">text of selected combo item</param>
        /// <returns>table whose name equals the text</returns>
        public static IStandaloneTable getCBOTable(IMap pMap, String tableName)
        {
            IStandaloneTableCollection pStandCollection = (IStandaloneTableCollection)pMap;
            IStandaloneTable pAloneTable = null;
            for (int i = 0; i < pStandCollection.StandaloneTableCount; i++)
            {
                if (tableName == pStandCollection.get_StandaloneTable(i).Name)
                {
                    pAloneTable = pStandCollection.get_StandaloneTable(i);
                }
            }
            return pAloneTable;
        }

        /*
        public static String exportDEM(ILayer layer)
        {
            IRasterLayer pRasterLayer = null;
            pRasterLayer = (IRasterLayer)layer;
            IDataset pDataset = pRasterLayer as IDataset;
            WorkspaceFactory pFact = (WorkspaceFactory)new RasterWorkspaceFactory();
            IWorkspace pWorkspace = pFact.OpenFromFile("", 0);

            String fileName = pRasterLayer.Name + System.DateTime.Now.ToString("mmss");
            IRasterBandCollection pRasBandCol = (IRasterBandCollection)pRasterLayer.Raster;
            pRasBandCol.SaveAs(fileName, pWorkspace, "GRID");

            String path = Environment.CurrentDirectory + "\\" + fileName;
            //MessageBox.Show(pRasterLayer.Name  + " has been exported");
            return path;
        }
         */
        /// <summary>
        /// get the path where the DEM is stored
        /// </summary>
        /// <param name="layer">the DEM Layer</param>
        /// <returns>DEM path</returns>
        public static String getDEMPath(ILayer layer)
        {
            IRasterLayer pRasterLayer = null;
            pRasterLayer = (IRasterLayer)layer;
            IDataset pDataset = (IDataset)pRasterLayer;
            return pDataset.Workspace.PathName + pDataset.Name;
        }

        /// <summary>
        /// check if pressed key is a number
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static Boolean isNumber(KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar) || (e.KeyChar == 46) || (e.KeyChar == 45))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
   
    }
}
